const getParameterByName = (name, url) => {
	if (!url) url = window.location.href;
	name = name.replace(/[\[\]]/g, '\\$&');
	var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
		results = regex.exec(url);
	if (!results) return null;
	if (!results[2]) return '';
	return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
const httpServer = (url, method, type, data) => {
	// var interfaceUrl='https://tapi.bkyh.top/';
	var interfaceUrl = 'https://bpapi.jinghushi.com/';
	var token = "";
	var mid = "";
	var b = '';
	var ver = uni.getStorageSync('ver');
	var lng = uni.getStorageSync('x');
	var lat = uni.getStorageSync('y');
	// #ifdef H5
	b = 'H5';
	if (getParameterByName('mid') != null) {
		uni.setStorageSync('mid', getParameterByName('mid'));
	}
	// #endif
	// #ifdef APP-PLUS
	b = 'APP';
	// #endif
	// #ifdef MP-WEIXIN
	b = 'WEIXIN';
	// #endif
	try {
		const value = uni.getStorageSync('token');
		const midval = uni.getStorageSync('mid');
		if (value) {
			token = value
		}
		mid = midval
	} catch (e) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('mid');
	}
	if (type == 'bonus') {
		interfaceUrl += 'bonus.php/'
	}
	if (type == 'creditshop') {
		interfaceUrl += 'creditshop.php/'
	}
	if (type == 'commission') {
		interfaceUrl += 'commission.php/'
	}
	if (type == 'coupon') {
		interfaceUrl += 'coupon.php/'
	}
	if (type == 'lottery') {
		interfaceUrl += 'lottery.php/'
	}
	if (type == 'member') {
		interfaceUrl += 'member.php/'
	}
	if (type == 'order') {
		interfaceUrl += 'order.php/'
	}
	if (type == 'salemsg') {
		interfaceUrl += 'salemsg.php/'
	}
	if (type == 'sale') {
		interfaceUrl += 'sale.php/'
	}
	if (type == 'shop') {
		interfaceUrl += 'shop.php/'
	}
	if (type == 'zhibo') {
		interfaceUrl += 'zhibo.php/'
	}
	if (type == 'group') {
		interfaceUrl += 'group.php/'
	}
	if (type == 'recmd') {
		interfaceUrl += 'recmd.php/'
	}
	if (type == 'saleadv') {
		interfaceUrl += 'saleadv.php/'
	}
	if (type == 'jjgy') {
		// interfaceUrl='http://api.test.jiujiubaopin.com/';
	}
	if (type == 'jd') {
		interfaceUrl += 'jd.php/'
	}
	if (type == 'supplier') {
		interfaceUrl += 'supplier.php/'
	}
	if (type == 'kf') {
		interfaceUrl += 'kf.php/'
	}
	if (type == 'douyin') {
		interfaceUrl += 'douyin.php/'
	}
	if (type == 'duoduo') {
		interfaceUrl += 'duoduo.php/'
	}

	let httpDefaultOpts = {
		url: interfaceUrl + url,
		data: data,
		method: method,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'token': token,
			'mid': mid,
			'b': b,
			'ver': ver,
			'lat': lat,
			'lng': lng
		},
		dataType: 'json',
	}

	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				let yinsi = uni.getStorageSync('yinsi');
				if (res[1].data.code == 403) {
					uni.showToast({
						title: res[1].data.data,
						icon: "none",
						duration: 3000
					})
				}
				if (res[1].data.code == 1002) {
					var Ver = uni.getStorageSync('ver');
					var Lng = uni.getStorageSync('x');
					var Lat = uni.getStorageSync('y');
					var TuanIDstr = uni.getStorageSync('JPinTuan');
					uni.clearStorage();
					uni.setStorageSync('mid', res[1].data.data.mid);
					uni.setStorageSync('JPinTuan', TuanIDstr);
					uni.setStorageSync('ver', Ver);
					uni.setStorageSync('x',Lng);
					uni.setStorageSync('y',Lat);
					uni.reLaunch({
						url: '/pages/public/login?mid=' + res[1].data.data.mid
					})
				}
				if (res[1].data.code == 300001) {
					uni.redirectTo({
						url: res[1].data.data
					})
				}
				uni.setStorageSync('yinsi', yinsi);
				resolve(res[1])
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
}

export default httpServer
